from manim import *
class math(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(60*2+1):       
            dot1=Dot([-500,5*(i-60),0]).set_opacity(0.5)
            dot2=Dot([500,5*(i-60),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(100*2+1):        
            dot3=Dot([(i-100)*5,-300,0]).set_opacity(0.5)
            dot4=Dot([(i-100)*5,300,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
        #镜头跟进效果 
        self.camera.frame.save_state()
        #题目出现
        text0 = Tex(r"\text{(2022)}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-37.5,2.5,0])
        text = Tex(r"\text{设}$f\left( x\right) $\text{在}$\left( -\infty ,+\infty \right) $\text{上有二阶连续导数,证明：}$f''\left( x\right) \geq 0$\text{的充分}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([4,2.5,0])
        text1 = Tex(r"\text{必要条件是对任意不同的实数a,b,都有}$f\left( \dfrac{a+b}{2}\right) \leq \dfrac{1}{b-a}\int _{a}^{b}f\left( x\right) dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-2.5,0])
        text10 = Tex(r"\text{必要条件是对任意不同的实数a,b,都有}$f\left( \dfrac{a+b}{2}\right) \leq \dfrac{1}{b-a}\int _{a}^{b}f\left( x\right) dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-2.5,0])
        
        self.play(Write(text0),Write(text),Write(text1),Write(text10),self.camera.frame.animate.scale(0.8))
        self.wait()
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        self.play(Restore(self.camera.frame))
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        self.play(text0.animate().move_to([-37.5,22.5,0]),text.animate().move_to([4,22.5,0]),text1.animate().move_to([0,17.5,0]) ,text10.animate().move_to([0,17.5,0])   )
        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=15,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([27.5, 22.5, 0])
        self.play(Create(rectangle))
        text2 = Tex(r"\text{泰勒不等式}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle,UP,buff=1)
        self.play(Write(text2))

        
        #下划线
        dot1=Dot([35,20,0])
        dot2=Dot([41,20,0])
        line=Line(dot1,dot2).set_color(YELLOW)
        line.set_stroke(width=30)  # 设置宽度为 30
        

        #下划线
        dot3=Dot([-40,15,0])
        dot4=Dot([-30,15,0])
        line1=Line(dot3,dot4).set_color(YELLOW)
        line1.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line1),Create(line))

        text3 = Tex(r"\text{设}$f\left( x\right) $\text{在}$\left( -\infty ,+\infty \right) $\text{上有二阶连续导数,证明：}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-7,22.5,0])
        

        text4 = Tex(r"\text{(I)已知}$f''\left( x\right) \geq 0,$\text{则对任意不同的实数a,b,都有}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-18,17.5,0])
        text40 = Tex(r"$f\left( \frac{a+b}{2}\right) \leq \frac{1}{b-a}\int _{a}^{b}f\left( x\right) dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([24,17.5,0])
        

        text5 = Tex(r"\text{(II)已知对任意不同的实数a,b,都有}"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-25.5,11,0])
        text50 = Tex(r"$f\left( \frac{a+b}{2}\right) \leq \frac{1}{b-a}\int _{a}^{b}f\left( x\right) dx,$\text{则}$f''\left( x\right) \geq 0$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([17,11,0])
        self.play(ReplacementTransform(line1,text5),ReplacementTransform(text1,text4),
                  ReplacementTransform(text10,text40),ReplacementTransform(text,text3)
                  ,ReplacementTransform(line,text50),FadeOut(rectangle,text2))
        self.wait(0.2)
        rectangle1=Rectangle(color=BLUE,fill_opacity=0.3,width=88,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle1.move_to([-4, 11, 0])
        self.play(Create(rectangle1))

        self.play(
            rectangle1.animate.move_to([10,11,0]).stretch_to_fit_width(30)
        )
        self.play(
            rectangle1.animate.move_to([32.5,11,0]).stretch_to_fit_width(15)
        )

        text6 = Tex(r"\text{逆向思维}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        self.play(Write(text6))
        text7 = Tex(r"\text{反证法}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(rectangle1,DOWN,buff=1)
        self.play(ReplacementTransform(text6,text7)
                  )
        self.wait(0.2)
        text8 = Tex(r"\text{假设存在}$f''\left( x_{0}\right)  <0$",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-35,5,0])
        self.play(ReplacementTransform(text7,text8)
                  )
        self.wait(0.2)
        rectangle2=Rectangle(color=BLUE,fill_opacity=0.3,width=50,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle2.move_to([-5, 22.5, 0])
        self.play(Create(rectangle2))
        text9 = Tex(r"\text{而}$f''\left( x\right)  $\text{在点}$x=x_0$\text{处连续}",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).next_to(text8,RIGHT,buff=1)
        self.play(ReplacementTransform(rectangle2,text9)
                  )
        self.wait(0.2)
        text10 = Tex(r"\text{局部保号性}",color=RED
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(Write(text10))
        text11 = Tex(r"\text{则在点}$x=x_0$\text{的某领域内都有}$f''\left( x\right) <0$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(ReplacementTransform(text10,text11)
                  )
        self.wait(0.2)

         #下划线
        dot5=Dot([-36,3,0])
        dot6=Dot([-24,3,0])
        line3=Line(dot5,dot6).set_color(BLUE)
        line3.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line3))

         #下划线

        dot7=Dot([-20,-2,0])
        dot8=Dot([20,-2,0])
        line4=Line(dot7,dot8).set_color(GREEN)
        line4.set_stroke(width=30)  # 设置宽度为 30
        self.play(Create(line4))

        text12 = Tex(r"\text{存在}$\delta  >0,$\text{使得当}$x\in \left[ x_{0}-\delta ,x_{0}+\delta \right] $\text{时,}$f''\left( x\right)  <0$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,0,0])
        self.play(ReplacementTransform(text11,text12)
                  ,FadeOut(line3,line4,rectangle1))
        self.wait(0.2)

        rectangle3=Rectangle(color=BLUE,fill_opacity=0.3,width=13,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle3.move_to([22, 0, 0])
        self.play(Create(rectangle3))
        
        rectangle4=Rectangle(color=BLUE,fill_opacity=0.3,width=30,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle4.move_to([10, 11, 0])
        self.play(Create(rectangle4))

        text13 = Tex(r"\text{取}$x_1,x_2$\text{不妨设}$x_1<x_2$",color=WHITE
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([5,-5,0])
        self.play(ReplacementTransform(rectangle4,text13)
                  )
        self.wait(0.2)

        text14 = Tex(r"$f\left( x\right) = f\left( \frac{x_1+x_2}{2}\right) +f'\left( \frac{x_1+x_2}{2}\right) \left( x-\frac{x_1+x_2}{2}\right)+ \frac{f''\left( \xi \right) }{2}\left( x-\frac{x_{1}+x_{2}}{2}\right) ^{2}$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-10,0])
        self.play(Write(text14))
        text15 = Tex(r"$f\left( x\right) < f\left( \frac{x_1+x_2}{2}\right) +f'\left( \frac{x_1+x_2}{2}\right) \left( x-\frac{x_1+x_2}{2}\right) $"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-15,0])
        self.play(ReplacementTransform(rectangle3,text15)
                  )
        self.wait(0.2)
        text11 = Tex(r"\text{两边积分}",color=YELLOW
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        self.play(Write(text11))
        text900 = Tex(r"$\int _{x_1}^{x_2}f\left( x\right) dx< \int ^{x_2}_{x_1}f\left( \frac{x_1+x_2}{2}\right) dx+\int _{x_1}^{x_2}f'\left( \frac{x_1+x_2}{2}\right) \left( x-\frac{x_1+x_2}{2}\right)dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        
        self.play(ReplacementTransform(text11,text900))
        text9000 = Tex(r"$f\left( \frac{x_{1}+x_{2}}{2}\right)  >\frac{1}{x_{2}-x_{1}}\int _{x_{1}}^{x_{2}}f\left( x\right) dx$"
                  
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([0,-20,0])
        
        self.play(ReplacementTransform(text900,text9000))
        rectangle4=Rectangle(color=GREEN,fill_opacity=0.3,width=75,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle4.move_to([-10, 11, 0])
        rectangle5=Rectangle(color=GREEN,fill_opacity=0.3,width=40,height=5
                            ,stroke_color=GREEN,stroke_width=20)
        rectangle5.move_to([0, -20, 0])
        self.play(Create(rectangle4),Create(rectangle5))
        self.wait()
